package com.autumn.mybatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.autumn.util.StringUtils;
import com.autumn.util.Time;

/**
 * 时间类型处理器
 * 
 * @author 杨昌国
 *
 *         2017-10-11 10:51:28
 */
public class TimeTypeHandler extends BaseTypeHandler<Time> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Time parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setString(i, parameter.toString());
	}

	@Override
	public Time getNullableResult(ResultSet rs, String columnName) throws SQLException {
		if (rs.wasNull()) {
			return null;
		}
		String value = rs.getString(columnName);
		if (StringUtils.isNullOrBlank(value)) {
			return null;
		}
		return Time.parse(value);
	}

	@Override
	public Time getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		if (rs.wasNull()) {
			return null;
		}
		String value = rs.getString(columnIndex);
		if (StringUtils.isNullOrBlank(value)) {
			return null;
		}
		return Time.parse(value);
	}

	@Override
	public Time getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		if (cs.wasNull()) {
			return null;
		}
		String value = cs.getString(columnIndex);
		if (StringUtils.isNullOrBlank(value)) {
			return null;
		}
		return Time.parse(value);
	}

}
